package com.chengyu.core.service.member.impl;

import cn.hutool.core.date.DateUtil;
import com.chengyu.core.mapper.BaseMapper;
import com.chengyu.core.mapper.PmsServicesMapper;
import com.chengyu.core.mapper.UmsMemberCollectionServicesMapper;
import com.chengyu.core.model.PmsServices;
import com.chengyu.core.model.UmsMember;
import com.chengyu.core.model.UmsMemberCollectionServices;
import com.chengyu.core.model.UmsMemberCollectionServicesExample;
import com.chengyu.core.service.member.MemberCollectServicesService;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @title  商品收藏
 * @author LeGreen
 * @date   2022/4/28
 */
@Service
public class MemberCollectServicesServiceImpl implements MemberCollectServicesService {
	
	@Autowired
	private UmsMemberCollectionServicesMapper memberCollectionServicesMapper;
	@Autowired
	private PmsServicesMapper servicesMapper;
	@Autowired
	private BaseMapper baseMapper;

	@Override
	public List<UmsMemberCollectionServices> getMemberCollectionServicesList(Integer memberId, Integer page, Integer pageSize) {
		PageHelper.startPage(page, pageSize);

		UmsMemberCollectionServicesExample example = new UmsMemberCollectionServicesExample();
		example.setOrderByClause("add_time desc");
		if(memberId != null){
			example.createCriteria().andMemberIdEqualTo(memberId);
		}
		return memberCollectionServicesMapper.selectByExample(example);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void collectOrNoServices(UmsMember member, Integer servicesId) {
		if(this.isCollectServices(member, servicesId)){
			//取消收藏
			UmsMemberCollectionServicesExample example = new UmsMemberCollectionServicesExample();
			example.createCriteria().andMemberIdEqualTo(member.getId()).andServicesIdEqualTo(servicesId);
			memberCollectionServicesMapper.deleteByExample(example);

			baseMapper.update("update pms_services set collection_num = collection_num-1 where id = "+servicesId);
		}else{
			//收藏
			UmsMemberCollectionServices collectionServices = new UmsMemberCollectionServices();
			collectionServices.setMemberId(member.getId());
			collectionServices.setMemberName(member.getNickname());
			collectionServices.setHeadImg(member.getHeadImg());

			PmsServices services = servicesMapper.selectByPrimaryKey(servicesId);
			collectionServices.setShopId(services.getShopId());
			collectionServices.setShopName(services.getShopName());
			collectionServices.setServicesId(services.getId());
			collectionServices.setServicesName(services.getTitle());
			collectionServices.setServicesMainImg(services.getMainImg());
			collectionServices.setPrice(services.getPrice());
			collectionServices.setAddTime(DateUtil.date());
			collectionServices.setUpdTime(collectionServices.getAddTime());
			memberCollectionServicesMapper.insertSelective(collectionServices);

			baseMapper.update("update pms_services set collection_num = collection_num+1 where id = "+servicesId);
		}
	}

	@Override
	public boolean isCollectServices(UmsMember member, Integer servicesId) {
		if(member == null){
			return false;
		}
		UmsMemberCollectionServicesExample example = new UmsMemberCollectionServicesExample();
		example.createCriteria().andMemberIdEqualTo(member.getId()).andServicesIdEqualTo(servicesId);
		return memberCollectionServicesMapper.countByExample(example) > 0;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void deleteCollectionServices(Integer collectServicesId) {
		memberCollectionServicesMapper.deleteByPrimaryKey(collectServicesId);
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class)
	public void deleteCollectionByServicesId(Integer servicesId) {
		UmsMemberCollectionServicesExample example = new UmsMemberCollectionServicesExample();
		example.createCriteria().andServicesIdEqualTo(servicesId);
		memberCollectionServicesMapper.deleteByExample(example);
	}
}